package com.g13.virtualpethospital.controller;

import com.g13.virtualpethospital.common.Result;
import com.g13.virtualpethospital.controller.request.QuestionRequest;
import com.g13.virtualpethospital.entity.CaseEntity;
import com.g13.virtualpethospital.entity.QuestionEntity;
import com.g13.virtualpethospital.service.QuestionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@ApiModel("题库操作相关接口")
@RequestMapping("/question")
public class QuestionBankController {

    @Autowired
    private QuestionService questionService;


    @PostMapping("/addQuestion")
    @ApiOperation("添加问题")
    @ResponseBody
    public Result<QuestionEntity> addQuestion(@RequestBody QuestionRequest request) {
        QuestionEntity entity = new QuestionEntity();
        entity.setAnswer(request.getAnswer());
        entity.setQuestionLevel1(request.getQuestionLevel1());
        entity.setQuestionLevel2(request.getQuestionLevel2());
        entity.setContent(request.getContent());
        entity.setOptions(request.getOptions());
        try {
            questionService.addQuestion(entity);
            return Result.success(entity);
        } catch (Exception e) {
            return Result.fail("失败");
        }
    }

    @DeleteMapping("/deleteQuestion")
    @ApiOperation("删除题目")
    @ResponseBody
    public Result<Integer> deleteQuestion(@RequestParam("questionId") String questionId) {
        try {
            QuestionEntity question = questionService.findQuestionById(questionId);
            long count = question.getUsedCnt();
            if(count >= 1 ){
                return Result.fail("问题已经被引用");
            }
            else {
                questionService.deleteQuestion(questionId);
                return Result.success(null);
            }
        } catch (Exception e) {
            return Result.fail("失败");
        }
    }


    @PutMapping("/modifyQuestion")
    @ApiOperation("编辑修改问题")
    @ResponseBody
    public Result<QuestionEntity>modifyQuestion(@NonNull @RequestBody QuestionEntity questionEntity) {
        try {
            questionService.updateQuestion(questionEntity,questionEntity.getQuestionId());
            return Result.success("修改成功", questionEntity);
        } catch (Exception e) {
            return Result.fail("修改失败");
        }
    }

    @GetMapping("/getQuestionByCatalog")
    @ApiOperation("按类别查找题目")
    @ResponseBody
    public Result<List<QuestionEntity>> getQuestionByCatalog(@RequestParam("questionLevel1") String questionLevel1,@RequestParam("questionLevel2") String questionLevel2) {

        List<QuestionEntity> results = questionService.findQuestionByType(questionLevel1,questionLevel2);
        if (results != null) {
            return Result.success(results);
        }
        return Result.fail("无该关键字病例");
    }

    @GetMapping("/getQuestionByContent")
    @ApiOperation("按题干查询题目")
    @ResponseBody
    public Result<List<QuestionEntity>> getQuestionByContent(@RequestParam("content") String content) {
        List<QuestionEntity> results = questionService.findQuestionByName(content);
        if (results != null) {
            return Result.success(results);
        }
        return Result.fail("无该关键字题目");
    }

    @GetMapping("/getAllQuestion")
    @ApiOperation("查询所有题目")
    @ResponseBody
    public Result<List<QuestionEntity>> getAllQuestion() {
        List<QuestionEntity> results = questionService.findAllQuestion();
        return Result.success(results);
    }

}
